package com.hsnr.findyourbuddy;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Serializable;
import java.util.HashMap;

import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

import android.content.Context;

import com.hsnr.findyourbuddy.SystemException.ErrorType;

public class Request implements Serializable {

    private static final long serialVersionUID = 1L;
    private char type;
    private String mobile;
    private HashMap<String, String> coordinates;

    public Request(char t, String mobile) {
	if (ConversationTypes.isConversationType(t)) {
	    type = t;
	} else {
	    throw new SystemException(ErrorType.INVALID_CONVERSATION_FORMAT);
	}

	setMobile(mobile);

    }

    public Request(char t) {
	if (ConversationTypes.isConversationType(t)) {
	    type = t;
	} else {
	    throw new SystemException(ErrorType.INVALID_CONVERSATION_FORMAT);
	}
    }

    /**
     * @return the coordinates
     */
    public HashMap<String, String> getCoordinates() {
	return coordinates;
    }

    /**
     * @return the type
     */
    public char getType() {
	return type;
    }

    /**
     * @return the mobile
     */
    public String getMobile() {
	return mobile;
    }

    /**
     * @param mobile
     *            the mobile to set
     */
    public void setMobile(String mobile) {
	this.mobile = mobile;
    }

    public String toJSON() {
	ObjectMapper mapper = new ObjectMapper();
	mapper.configure(JsonGenerator.Feature.AUTO_CLOSE_TARGET, false);
	try {
	    File temp = File.createTempFile("jsonResponse", ".tmp");
	    mapper.writeValue(temp, this);
	    BufferedReader fr = new BufferedReader(new FileReader(temp));
	    StringBuffer sb = new StringBuffer();
	    String s;
	    while ((s = fr.readLine()) != null) {
		sb.append(s);
	    }
	    fr.close();
	    return sb.toString();
	} catch (IOException e) {
	    e.printStackTrace();
	}
	return null;
    }

    public static Request fromJSON(String s, Context context) throws JsonParseException, JsonMappingException, IOException {
        File outputDir = context.getCacheDir(); // context being the
        // Activity pointer
File temp = File.createTempFile("jsonRequest", ".tmp", outputDir);

	BufferedWriter bw = new BufferedWriter(new FileWriter(temp));
	bw.write(s);
	bw.close();

	ObjectMapper mapper = new ObjectMapper();
	mapper.configure(JsonGenerator.Feature.AUTO_CLOSE_TARGET, false);
	Request r = null;
	try {
	   r = mapper.readValue(temp, BroadcastRequest.class);
	} catch (JsonMappingException e) {
//	    try {
//		r = mapper.readValue(temp, ContactRequest.class);
//	    } catch (JsonMappingException e1) {
//		try {
//		    r = mapper.readValue(temp, SingleRequest.class);
//		} catch (JsonMappingException e2) {
//		    throw new SystemException(ErrorType.INVALID_CONVERSATION_FORMAT);
//		}
//	    }
	}
	return r;
	
    }
}
